#include "setting_gui.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define TITLE_HEIGHT 40
#define WIFI_INFO_FMT \
LV_SYMBOL_HOME"MAC:%s\n"\
"\xEF\x88\x93""频段:%s\n"\
"\xEF\x8B\x8e""比特率:%s\n"\
LV_SYMBOL_WIFI"信号强度:%d%%"

LV_IMG_DECLARE(butterfly)
LV_IMG_DECLARE(poweroff)
LV_IMG_DECLARE(reboot)
LV_IMG_DECLARE(wifi_icon)
LV_IMG_DECLARE(wifi_1)
LV_IMG_DECLARE(wifi_2)
LV_IMG_DECLARE(wifi_3)
LV_IMG_DECLARE(wifi_4)

LV_FONT_DECLARE(lv_font_mh_12)
LV_FONT_DECLARE(lv_font_mh_14)
LV_FONT_DECLARE(lv_font_mh_16)
LV_FONT_DECLARE(lv_font_mh_18)
LV_FONT_DECLARE(lv_font_mh_20)
LV_FONT_DECLARE(lv_font_mh_22)
LV_FONT_DECLARE(lv_font_mh_24)

struct title
{
    lv_obj_t *cont;
    lv_obj_t *label;
    lv_obj_t *back_btn;
};

static void setting_gui_wifi_panel_create(void);
static void setting_gui_wifi_mssage_box_create(lv_obj_t *wifi_btn);

static void brightness_slider_event_cb(lv_event_t *e);
static void volume_slider_event_cb(lv_event_t *e);
static void drag_event_handler(lv_event_t *e);
static void poweroff_reboot_btn_event_cb(lv_event_t *e);
static void wifi_btn_event_cb(lv_event_t *e);
static void title_back_event_cb(lv_event_t *e);
static void wifi_list_btn_event_cb(lv_event_t *e);
static void wifi_list_btn_messagebox_top_layer_event_cb(lv_event_t *e);
static void pwd_textarea_event_cb(lv_event_t *e);
static void pwd_btn_event_cb(lv_event_t *e);
static void connect_btn_event_cb(lv_event_t *e);
static void auto_del_obj_timer_cb(lv_timer_t *t);

// gui回调函数集
static gui_opt_t gui_opts;

// LCD分辨率
static uint32_t lcd_w = 480;
static uint32_t lcd_h = 272;

//背景图片
static lv_obj_t *back_img;

//顶部标题栏
static struct title title;

//背光滑块小工具
static lv_obj_t *backlight_slider;

//音量滑块小工具
static lv_obj_t *volume_slider;

//退出按钮
static lv_obj_t *exit_btn;

//关机按钮
static lv_obj_t *poweroff_btn;

//重启按钮
static lv_obj_t *reboot_btn;

//wifi界面字标题
static lv_obj_t *c_title;

//wifi界面列表list
static lv_obj_t *wifi_list;

//保存已连接的wifi list btn
static lv_obj_t *connected_wifi_btn;

//wifi连接中提示框容器
static lv_obj_t *connecting_cont;

//网络信息面板标签组
static lv_obj_t *net_label[7];

//网络信息面板标签文本头
static const char *net_label_header[7] = 
{
    "已连接热点: ",
    "网卡MAC: ",
    "IP地址: ",
    "已接收数据包: ",
    "已发送数据包: ",
    "已接收: ",
    "已发送: ",
};

void setting_gui_init(gui_opt_t *opts)
{
    if(opts != NULL)
        memcpy(&gui_opts, opts, sizeof(gui_opt_t));          //拷贝回调函数

    // 获取显示设备分辨率
    lcd_w = lv_disp_get_hor_res(NULL);
    lcd_h = lv_disp_get_ver_res(NULL);

    //创建背景图片
    back_img = lv_img_create(lv_scr_act());
    lv_obj_set_size(back_img, lcd_w, lcd_h);
    lv_img_set_src(back_img, "S:./res/background/settingbg.bin");
    lv_obj_center(back_img);
    lv_obj_clear_flag(back_img, LV_OBJ_FLAG_SCROLLABLE);       //设置不可滚动

    //创建顶部标题栏
    title.cont = lv_obj_create(back_img);
    lv_obj_set_size(title.cont, lcd_w, TITLE_HEIGHT);
    lv_obj_set_style_radius(title.cont, 0, LV_STATE_DEFAULT);                   //圆角0
    lv_obj_set_style_border_width(title.cont, 0, LV_STATE_DEFAULT);     //边框宽度0
    lv_obj_set_style_bg_color(title.cont, lv_color_black(), LV_STATE_DEFAULT);      //背景图片
    lv_obj_set_style_bg_opa(title.cont, LV_OPA_70, LV_STATE_DEFAULT);       //背景透明
    lv_obj_clear_flag(title.cont, LV_OBJ_FLAG_SCROLLABLE);      //禁用滚动

    title.label = lv_label_create(title.cont);
    lv_obj_set_height(title.label, TITLE_HEIGHT);
    lv_label_set_text(title.label, "设置");
    lv_obj_set_style_text_color(title.label, lv_color_white(), LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(title.label, &lv_font_mh_24, LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(title.label, LV_OPA_80, LV_STATE_DEFAULT);
    lv_obj_center(title.label);

    title.back_btn = lv_btn_create(title.cont);
    lv_obj_set_height(title.back_btn, TITLE_HEIGHT);
    lv_obj_align(title.back_btn, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_bg_color(title.back_btn, lv_color_white(), LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(title.back_btn, LV_OPA_40, LV_STATE_PRESSED);     //容器背景透明度(按下时)
    lv_obj_set_style_bg_opa(title.back_btn, LV_OPA_TRANSP, LV_STATE_DEFAULT);     //容器背景透明
    lv_obj_set_style_border_opa(title.back_btn, LV_OPA_TRANSP, LV_STATE_DEFAULT);     //容器边框透明
    lv_obj_add_flag(title.back_btn, LV_OBJ_FLAG_HIDDEN);      //隐藏
    lv_obj_add_event_cb(title.back_btn, title_back_event_cb, LV_EVENT_SHORT_CLICKED, NULL);
    lv_obj_t *back_label = lv_label_create(title.back_btn);
    lv_label_set_text(back_label, "\xEF\x84\x92");        //返回标签
    lv_obj_set_style_text_color(back_label, lv_color_white(), LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(back_label, &lv_font_mh_24, LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(back_label, LV_OPA_80, LV_STATE_DEFAULT);
    lv_obj_center(back_label);

    //创建背光和音量滑块小工具
    lv_obj_t *slider[2];
    const char *title_name[] = {"背光", "音量"};

    for(int i = 0; i < 2; i++)
    {
        slider[i] = lv_slider_create(back_img);
        lv_obj_set_size(slider[i], 400, 15);
        lv_obj_set_pos(slider[i], 100, 100 + i * 60);
        
        //创建滑块小工具标题
        lv_obj_t *label = lv_label_create(back_img);        //创建滑动条标签
        lv_obj_set_style_text_font(label, &lv_font_mh_16, LV_STATE_DEFAULT);        //设置字体
        lv_label_set_text(label, title_name[i]);
        lv_obj_align_to(label, slider[i], LV_ALIGN_OUT_LEFT_MID, -20, 0);

        //创建滑块小工具数值标签
        label = lv_label_create(back_img);        //创建滑动条标签
        lv_obj_set_style_text_font(label, &lv_font_mh_16, LV_STATE_DEFAULT);        //设置字体
        lv_label_set_text(label, "50%");
        lv_obj_align_to(label, slider[i], LV_ALIGN_OUT_RIGHT_MID, 20, 0);

        lv_obj_set_user_data(slider[i], label);     //绑定对应滑块小工具
    }

    backlight_slider = slider[0];
    volume_slider = slider[1];

    //设置滑块小工具范围
    lv_slider_set_range(backlight_slider, 5, 100);
    lv_slider_set_range(volume_slider, 0, 100);

    //设置滑块小工具初始值
    if(gui_opts.get_brightness != NULL)
    {
        int value = gui_opts.get_brightness();
        lv_slider_set_value(backlight_slider, value, LV_ANIM_ON);
        lv_label_set_text_fmt(lv_obj_get_user_data(backlight_slider), "%d%%", value);
    }
    if(gui_opts.get_volume != NULL)
    {
        int value = gui_opts.get_volume();
        lv_slider_set_value(volume_slider, value, LV_ANIM_ON);
        lv_label_set_text_fmt(lv_obj_get_user_data(volume_slider), "%d%%", value);
    }

    //设置滑块小工具事件回调
    lv_obj_add_event_cb(backlight_slider, brightness_slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_add_event_cb(volume_slider, volume_slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    //创建关机和重启按钮
    lv_obj_t *btn[2];
    const lv_img_dsc_t *img_dsc[2] = {&poweroff, &reboot};
    const char *btn_name[] = {"关机", "重启"};

    for(int i = 0; i < 2; i++)
    {
        btn[i] = lv_btn_create(back_img);
        lv_obj_set_size(btn[i], 128, 158);
        lv_obj_set_style_bg_color(btn[i], lv_color_hex(0x8a8a8a), LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(btn[i], LV_OPA_40, LV_STATE_DEFAULT);
        lv_obj_add_event_cb(btn[i], poweroff_reboot_btn_event_cb, LV_EVENT_SHORT_CLICKED, NULL);

        lv_obj_t *img = lv_img_create(btn[i]);
        lv_img_set_src(img, img_dsc[i]);
        lv_obj_align(img, LV_ALIGN_TOP_MID, 0, -10);

        lv_obj_t *label = lv_label_create(btn[i]);
        lv_obj_set_style_text_font(label, &lv_font_mh_16, LV_STATE_DEFAULT);        //设置字体
        lv_obj_set_style_text_color(label, lv_color_hex(0x707070), LV_STATE_DEFAULT);
        lv_label_set_text(label, btn_name[i]);
        lv_obj_align(label, LV_ALIGN_BOTTOM_MID, 0, 10);
    }
    poweroff_btn = btn[0];
    reboot_btn = btn[1];

    //设置按钮对齐
    lv_obj_align(reboot_btn, LV_ALIGN_BOTTOM_RIGHT, -80, -40);
    lv_obj_align_to(poweroff_btn, reboot_btn, LV_ALIGN_OUT_LEFT_MID, -30, 0);

    //创建网络信息面板容器
    lv_obj_t *net_cont = lv_obj_create(back_img);       
    lv_obj_set_size(net_cont, 500, 300);
    lv_obj_align(net_cont, LV_ALIGN_BOTTOM_LEFT, 50, -40);
    lv_obj_clear_flag(net_cont, LV_OBJ_FLAG_SCROLLABLE);            //禁止滚动
    lv_obj_set_style_bg_color(net_cont, lv_color_black(), LV_STATE_DEFAULT);        //容器背景黑色
    lv_obj_set_style_bg_opa(net_cont, LV_OPA_40, LV_STATE_DEFAULT);     //容器背景透明度
    lv_obj_set_style_border_opa(net_cont, LV_OPA_TRANSP, LV_STATE_DEFAULT);     //容器边框透明

    //创建网络信息面板labei
    for(int i =0; i < 7; i++)
    {
        lv_obj_t *label = lv_label_create(net_cont);
        lv_obj_set_style_text_font(label, &lv_font_mh_20,LV_STATE_DEFAULT);     //设置字体
        lv_obj_set_style_text_color(label, lv_color_hex(0xcdcdcd), LV_STATE_DEFAULT);       //字体颜色
        lv_label_set_text(label, net_label_header[i]);
        net_label[i] = label;

        if(i != 0)
            lv_obj_align_to(label, net_label[i - 1], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
        else
            lv_obj_align(label, LV_ALIGN_TOP_LEFT, 10, 0);
    }

    //创建wifi按钮
    lv_obj_t *wifi_btn_cont = lv_obj_create(net_cont);
    lv_obj_set_size(wifi_btn_cont, 50, 50);
    lv_obj_align(wifi_btn_cont, LV_ALIGN_BOTTOM_RIGHT, 0, 0);
    lv_obj_set_style_bg_color(wifi_btn_cont, lv_color_white(), LV_STATE_PRESSED);        //容器背景白色(按下时)
    lv_obj_set_style_bg_opa(wifi_btn_cont, LV_OPA_40, LV_STATE_PRESSED);     //容器背景透明度(按下时)
    lv_obj_set_style_bg_opa(wifi_btn_cont, LV_OPA_TRANSP, LV_STATE_DEFAULT);     //容器背景透明
    lv_obj_set_style_border_opa(wifi_btn_cont, LV_OPA_TRANSP, LV_STATE_DEFAULT);     //容器边框透明
    lv_obj_clear_flag(wifi_btn_cont, LV_OBJ_FLAG_SCROLLABLE);       //设置不可滚动

    lv_obj_t *img = lv_img_create(wifi_btn_cont);
    lv_img_set_src(img, &wifi_icon);
    lv_obj_center(img);
    lv_obj_add_flag(img, LV_OBJ_FLAG_EVENT_BUBBLE | LV_OBJ_FLAG_CLICKABLE);         //事件同时传播到父对象
    lv_obj_add_event_cb(img, wifi_btn_event_cb, LV_EVENT_SHORT_CLICKED, NULL);
    lv_obj_set_user_data(title.back_btn, img);        //设置用户数据

    //创建退出按钮
    exit_btn = lv_obj_create(back_img);
    lv_obj_set_size(exit_btn, 70, 70);
    lv_obj_set_pos(exit_btn, 935, 267);
    lv_obj_clear_flag(exit_btn, LV_OBJ_FLAG_SCROLLABLE);            //禁止滚动
    lv_obj_set_style_radius(exit_btn, 35, LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(exit_btn, lv_color_hex(0xffffff), LV_STATE_DEFAULT);
    lv_obj_set_style_border_opa(exit_btn, LV_OPA_TRANSP, LV_STATE_DEFAULT);     //A边框透明
    lv_obj_set_style_bg_opa(exit_btn, LV_OPA_50, LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(exit_btn, LV_OPA_90, LV_STATE_PRESSED);

    img = lv_img_create(exit_btn);
    lv_img_set_src(img, &butterfly);
    lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_flag(img, LV_OBJ_FLAG_EVENT_BUBBLE);

    lv_obj_add_event_cb(exit_btn, drag_event_handler, LV_EVENT_PRESSING, NULL);
    if (gui_opts.exit_cb != NULL)
        lv_obj_add_event_cb(exit_btn, gui_opts.exit_cb, LV_EVENT_SHORT_CLICKED, NULL);
}

/*
 *@brief 更新网络信息到UI
 *@param info 网络信息
 */
void setting_gui_update_net_info(NetInfo *info)
{
    if(info == NULL)
        return;

    if(strcmp(info->essid, "null") != 0)
    {
        lv_label_set_text_fmt(net_label[0], "%s%s", net_label_header[0], info->essid);
        lv_obj_set_style_text_color(net_label[0], lv_color_hex(0x59a3fd), LV_STATE_DEFAULT);        //蓝色系文本
    }
    else
    {
        lv_label_set_text(net_label[0], "未连接热点!");
        lv_obj_set_style_text_color(net_label[0], lv_color_hex(0xe1663a), LV_STATE_DEFAULT);        //红色系文本
    }

    lv_label_set_text_fmt(net_label[1], "%s%s", net_label_header[1], info->address);
    lv_label_set_text_fmt(net_label[2], "%s%s", net_label_header[2], info->ip_addr);
    lv_label_set_text_fmt(net_label[3], "%s%d", net_label_header[3], info->rx_packets);
    lv_label_set_text_fmt(net_label[4], "%s%d", net_label_header[4], info->tx_packets);
    lv_label_set_text_fmt(net_label[5], "%s%d kB", net_label_header[5], info->rx_bytes / 1024);
    lv_label_set_text_fmt(net_label[6], "%s%d kB", net_label_header[6], info->tx_bytes / 1024);
}

/*
 *@brief 向wifi列表添加一个wifi
 *@param info wifi信息
 */
void setting_gui_list_add_wifi(WifiInfo *info)
{
    static const lv_img_dsc_t *wifi_signal_level_img[4] = {&wifi_1, &wifi_2, &wifi_3, &wifi_4};

    if(wifi_list == NULL)
        return;

    WifiInfo *wifi = calloc(sizeof(WifiInfo), 1);
    memcpy(wifi, info, sizeof(WifiInfo));

    // 添加一个wifi按钮到list
    lv_obj_t *btn = lv_list_add_btn(wifi_list, NULL, wifi->essid);
    lv_obj_set_style_bg_opa(btn, LV_OPA_TRANSP, LV_STATE_DEFAULT);     // 设置背景透明
    lv_obj_set_style_bg_opa(btn, LV_OPA_30, LV_STATE_PRESSED);     // 设置背景透明度(按下时)
    lv_obj_set_style_text_color(btn, lv_color_hex(0xcdcdcd), LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(btn, &lv_font_mh_22, LV_STATE_DEFAULT);        //设置字体
    lv_obj_add_event_cb(btn, wifi_list_btn_event_cb, LV_EVENT_SHORT_CLICKED, NULL);

    //创建具有密钥的wifi "锁"图标或者 ""已连接"图标
    lv_obj_t *lock = lv_label_create(btn);
    lv_obj_set_style_text_font(lock, &lv_font_mh_22, LV_STATE_DEFAULT);        //设置字体
    lv_obj_set_style_text_color(lock, lv_color_hex(0xcdcdcd), LV_STATE_DEFAULT);
    lv_obj_align(lock, LV_ALIGN_RIGHT_MID, 0, 0);
    if(info->is_connected == true)
    {
        lv_label_set_text(lock, "\xEF\x80\x8c");       //已连接图标
        connected_wifi_btn = btn;       //保存已连接的wifi btn
    }
    else if(info->encryption_key == true)
        lv_label_set_text(lock, "\xEF\x80\xa3");       //锁图标
    else
        lv_label_set_text(lock, " ");       //空图标

    lv_obj_set_user_data(btn, lock);            //保存label
    lv_obj_set_user_data(lock, wifi);            //保存wifi信息

    //创建wifi信号强度图标
    lv_obj_t *signal_level_img = lv_img_create(btn);
    int index = (info->level - 1) / 25;
    lv_img_set_src(signal_level_img, wifi_signal_level_img[index]);     //设置wifi信号强度图标
    lv_obj_align(signal_level_img, LV_ALIGN_RIGHT_MID, 0, 0);
}

void setting_gui_release(void)
{
    lv_obj_del(back_img);
}

/*
 *@brief 创建wifi面板
 */
static void setting_gui_wifi_panel_create(void)
{
    //创建子标题
    c_title = lv_label_create(back_img);
    lv_label_set_text(c_title, "WIFI "LV_SYMBOL_RIGHT);
    lv_obj_set_size(c_title, lcd_w, 45);
    lv_obj_align_to(c_title, title.cont, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
    lv_obj_set_style_bg_opa(c_title, LV_OPA_90, LV_STATE_DEFAULT);     // 设置背景透明度
    lv_obj_set_style_bg_color(c_title, lv_color_black(), LV_STATE_DEFAULT);        //背景黑色
    lv_obj_set_style_text_color(c_title, lv_color_hex(0xcdcdcd), LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(c_title, &lv_font_mh_24, LV_STATE_DEFAULT);        //设置字体

    //创建list
    wifi_list = lv_list_create(back_img);
    lv_obj_set_size(wifi_list, lcd_w, lcd_h - TITLE_HEIGHT - 45);
    lv_obj_align_to(wifi_list, c_title, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
    lv_obj_set_style_bg_opa(wifi_list, LV_OPA_90, LV_STATE_DEFAULT);     // 设置背景透明度
    lv_obj_set_style_bg_color(wifi_list, lv_color_black(), LV_STATE_DEFAULT);        //背景黑色
    lv_obj_set_style_border_opa(wifi_list, LV_OPA_TRANSP, LV_STATE_DEFAULT); // 设置边框透明
    lv_obj_set_style_radius(wifi_list, 0, LV_STATE_DEFAULT);        //边框圆角为0

    if(gui_opts.wlan_scan != NULL)
        gui_opts.wlan_scan();        
}

/*
 *@brief 创建某个wifi的模态消息框
 *@param wifi_btn wifi list中的某个btn
 */
static void setting_gui_wifi_mssage_box_create(lv_obj_t *wifi_btn)
{
    if(wifi_btn == NULL)
        return;

    lv_obj_t*btn_label = lv_obj_get_user_data(wifi_btn);
    WifiInfo *info = lv_obj_get_user_data(btn_label);
    if(info == NULL)
        return;

    lv_obj_t *top_layer = lv_layer_top();       //获取top层
    lv_obj_set_style_bg_color(top_layer, lv_palette_main(LV_PALETTE_GREY), LV_STATE_DEFAULT);        //top层背景颜色灰色
    lv_obj_set_style_bg_opa(top_layer, LV_OPA_50, LV_STATE_DEFAULT);     //top层背景透明度
    lv_obj_set_user_data(top_layer, NULL);

    //创建模态容器
    lv_obj_add_flag(top_layer, LV_OBJ_FLAG_CLICKABLE);      //设置模态层可点击
    lv_obj_t *cont = lv_obj_create(top_layer);      //将容器创建在top层上
    lv_obj_set_size(cont, 500, 400);
    lv_obj_center(cont);
    lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);        //禁止滚动
    lv_obj_set_style_bg_color(cont, lv_color_black(), LV_STATE_DEFAULT);        //容器背景黑色
    lv_obj_set_style_bg_opa(cont, LV_OPA_90, LV_STATE_DEFAULT);     //容器背景透明度
    lv_obj_set_style_border_opa(cont, LV_OPA_TRANSP, LV_STATE_DEFAULT);     //容器边框透明
    lv_obj_set_user_data(cont, NULL);

    //创建窗口标题
    lv_obj_t *title_label = lv_label_create(cont);
    lv_label_set_text_fmt(title_label, "连接到\"%s\"", info->essid);
    lv_obj_set_style_text_font(title_label, &lv_font_mh_22, LV_STATE_DEFAULT);      //设置字体
    lv_obj_set_style_text_color(title_label, lv_color_hex(0xe6e6e6), LV_STATE_DEFAULT);     //字体颜色
    lv_obj_align(title_label, LV_ALIGN_TOP_LEFT, -10, -20);     //标题对齐左上角

    //创建wifi信息label
    lv_obj_t *info_label = lv_label_create(cont);
    lv_obj_set_style_text_font(info_label, &lv_font_mh_18, LV_STATE_DEFAULT);      //设置字体
    lv_obj_set_style_text_color(info_label, lv_color_hex(0x8a8a8a), LV_STATE_DEFAULT);     //字体颜色
    lv_label_set_text_fmt(info_label, WIFI_INFO_FMT, info->address, info->freq, info->bit_rates, info->level);
    lv_obj_align_to(info_label, title_label, LV_ALIGN_OUT_BOTTOM_LEFT, 10, 0);

    //创建连接/断开连接按钮
    lv_obj_t *connect_btn = lv_btn_create(cont);
    lv_obj_set_size(connect_btn, 450, 80);
    lv_obj_align(connect_btn, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_add_event_cb(connect_btn, connect_btn_event_cb, LV_EVENT_SHORT_CLICKED, wifi_btn);

    lv_obj_t *connect_btn_label = lv_label_create(connect_btn);
    lv_obj_set_style_text_font(connect_btn_label, &lv_font_mh_22, LV_STATE_DEFAULT);      //设置字体
    lv_obj_set_style_text_color(connect_btn_label, lv_color_hex(0xdce2e9), LV_STATE_DEFAULT);     //字体颜色
    lv_label_set_text(connect_btn_label, "连接");
    lv_obj_center(connect_btn_label);
    lv_obj_set_user_data(connect_btn, connect_btn_label);

    if(info->is_connected == true)
        lv_label_set_text(connect_btn_label, "断开连接");                                                                                                           
    else if(info->encryption_key == true)               //创建wifi密码文本框与密码可视按钮          
    {   
        lv_obj_t *pwd_textarea = lv_textarea_create(cont);
        lv_obj_set_size(pwd_textarea, 400, 60);
        lv_textarea_set_placeholder_text(pwd_textarea, "请输入密码......");     //设置占位文本
        lv_textarea_set_password_mode(pwd_textarea, true);          //设为密码模式
        lv_textarea_set_one_line(pwd_textarea, true);           //设置单行模式
        lv_obj_set_style_text_font(pwd_textarea, &lv_font_mh_22, LV_STATE_DEFAULT);      //设置字体
        lv_obj_set_style_text_color(pwd_textarea, lv_color_hex(0x2c2c2c), LV_STATE_DEFAULT);     //字体颜色
        lv_obj_clear_flag(pwd_textarea, LV_OBJ_FLAG_SCROLLABLE);        //禁止滚动
        lv_obj_align_to(pwd_textarea, connect_btn, LV_ALIGN_OUT_TOP_LEFT, 0, -25);
        lv_obj_add_event_cb(pwd_textarea, pwd_textarea_event_cb, LV_EVENT_SHORT_CLICKED, cont);          //设置单击事件回调函数

        if(info->id >= 0)
            lv_textarea_set_text(pwd_textarea, info->psk);          //若是已保存的wifi则自动填入密码

        lv_obj_set_user_data(top_layer, pwd_textarea);

        //创建密码可视切换按钮
        lv_obj_t *pwd_btn = lv_btn_create(cont);
        lv_obj_set_size(pwd_btn, 60, 60);
        lv_obj_set_style_bg_color(pwd_btn, lv_color_white(), LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(pwd_btn, LV_OPA_40, LV_STATE_PRESSED);     //背景透明度(按下时)
        lv_obj_set_style_bg_opa(pwd_btn, LV_OPA_TRANSP, LV_STATE_DEFAULT);     //背景透明
        lv_obj_set_style_border_opa(pwd_btn, LV_OPA_TRANSP, LV_STATE_DEFAULT);     //边框透明
        lv_obj_align_to(pwd_btn, pwd_textarea, LV_ALIGN_OUT_RIGHT_MID, 7, 0);
        
        lv_obj_t *pwd_btn_label = lv_label_create(pwd_btn);
        lv_obj_set_style_text_font(pwd_btn_label, &lv_font_mh_24, LV_STATE_DEFAULT);      //设置字体
        lv_obj_set_style_text_color(pwd_btn_label, lv_color_hex(0x8a8a8a), LV_STATE_DEFAULT);     //字体颜色
        lv_label_set_text(pwd_btn_label, LV_SYMBOL_EYE_CLOSE);
        lv_obj_center(pwd_btn_label);

        lv_obj_set_user_data(pwd_btn, pwd_btn_label);
        lv_obj_add_event_cb(pwd_btn, pwd_btn_event_cb, LV_EVENT_SHORT_CLICKED, pwd_textarea);

    }

    //为top layer添加点击事件处理函数
    lv_obj_add_event_cb(top_layer, wifi_list_btn_messagebox_top_layer_event_cb, LV_EVENT_CLICKED, cont);
}

/*
 *@brief 创建wifi连接中模态提示框
 */
void setting_gui_connecting_cont_create(void)
{
    lv_obj_t *top_layer = lv_layer_top();
    lv_obj_set_style_bg_color(top_layer, lv_palette_main(LV_PALETTE_GREY), LV_STATE_DEFAULT);        //top层背景颜色灰色
    lv_obj_set_style_bg_opa(top_layer, LV_OPA_50, LV_STATE_DEFAULT);     //top层背景透明度

    connecting_cont = lv_obj_create(top_layer);      //将容器创建在top层上
    lv_obj_set_size(connecting_cont, 300, 100);
    lv_obj_center(connecting_cont);
    lv_obj_clear_flag(connecting_cont, LV_OBJ_FLAG_SCROLLABLE);        //禁止滚动
    lv_obj_set_style_bg_color(connecting_cont, lv_color_black(), LV_STATE_DEFAULT);        //容器背景黑色
    lv_obj_set_style_bg_opa(connecting_cont, LV_OPA_90, LV_STATE_DEFAULT);     //容器背景透明度
    lv_obj_set_style_border_opa(connecting_cont, LV_OPA_TRANSP, LV_STATE_DEFAULT);     //容器边框透明
    
    lv_obj_t *label = lv_label_create(connecting_cont);
    lv_obj_set_style_text_font(label, &lv_font_mh_24, LV_STATE_DEFAULT);      //设置字体
    lv_obj_set_style_text_color(label, lv_color_hex(0x28cbe4), LV_STATE_DEFAULT);     //字体颜色
    lv_label_set_text(label, "正在连接....");
    lv_obj_center(label);

    lv_obj_set_user_data(connecting_cont, label);
}   

/*
 *@brief 通知UI，连接成功/失败
 *@param status true - 成功    flase - 失败
 */
void setting_gui_connect_set_status(bool status)
{
    lv_obj_t *label = lv_obj_get_user_data(connecting_cont);

    if(status == true)
        lv_label_set_text(label, "正在获取IP地址....");
    else
        lv_label_set_text(label, "连接失败");
}

/*
 *@brief 通知UI，获取IP地址成功/失败
 *@param status true - 成功    flase - 失败
 */
void setting_gui_get_ip_set_status(bool status)
{
    lv_obj_t *label = lv_obj_get_user_data(connecting_cont);

    if(status == true)
        lv_label_set_text(label, "连接成功");
    else
        lv_label_set_text(label, "无法获取IP地址");
}

/*
 *@brief 通知UI，连接wifi动作结束
  *@param status true - 连接成功    flase - 连接失败
 */
void setting_gui_connect_over(bool status)
{
    //创建定时器，自动删除对象
    if(connecting_cont != NULL)
    {
        lv_timer_t  *timer = lv_timer_create(auto_del_obj_timer_cb, 2000, connecting_cont);
        lv_timer_set_repeat_count(timer, 1);        //单次定时器
    }

    lv_event_send(lv_layer_top(), LV_EVENT_CLICKED, NULL);        //向顶层发送click事件，删除wifi信息面板(如果有)

    lv_obj_t *wifi_btn_label = lv_obj_get_user_data(connected_wifi_btn);
    WifiInfo *info = lv_obj_get_user_data(wifi_btn_label);

    if(status == true)
    {
        lv_label_set_text(wifi_btn_label, "\xEF\x80\x8c");       //已连接图标
        info->is_connected = true;
    }
    else
    {
        lv_label_set_text(wifi_btn_label, "\xEF\x80\xa3");       //锁图标
        info->is_connected = false;
        connected_wifi_btn = NULL;
    }
}

/*
 *@brief 背光滑块小工具事件回调函数
 */
static void brightness_slider_event_cb(lv_event_t *e)
{
    lv_obj_t *slider = lv_event_get_target(e);
    lv_obj_t *label = lv_obj_get_user_data(slider);

    int value = lv_slider_get_value(slider);

    lv_label_set_text_fmt(label, "%d%%", value);

    if(gui_opts.set_brightness != NULL)
        gui_opts.set_brightness(value);
}

/*
 *@brief 音量滑块小工具事件回调函数
 */
static void volume_slider_event_cb(lv_event_t *e)
{
    lv_obj_t *slider = lv_event_get_target(e);
    lv_obj_t *label = lv_obj_get_user_data(slider);

    int value = lv_slider_get_value(slider);

    lv_label_set_text_fmt(label, "%d%%", value);

    if(gui_opts.set_volume != NULL)
        gui_opts.set_volume(value);
}

/*
 *@brief 长按拖动控件事件函数
 */
static void drag_event_handler(lv_event_t *e)
{
    lv_obj_t *obj = lv_event_get_target(e);

    lv_indev_t *indev = lv_indev_get_act();
    if (indev == NULL)
        return;

    lv_point_t vect;
    lv_indev_get_vect(indev, &vect);

    lv_coord_t x = lv_obj_get_x(obj) + vect.x;
    lv_coord_t y = lv_obj_get_y(obj) + vect.y;
    lv_obj_set_pos(obj, x, y);
}

/*
 *@brief  关机重启按钮事件回调函数
 */
static void poweroff_reboot_btn_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);

    if(btn == poweroff_btn)
    {
        if(gui_opts.poweroff != NULL)
            gui_opts.poweroff();
    }
    else if(btn == reboot_btn)
    {
        if(gui_opts.reboot != NULL)
            gui_opts.reboot();
    }
}

/*
 *@brief  wifi btn点击事件回调函数
 */
static void wifi_btn_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);

    lv_obj_clear_flag(btn, LV_OBJ_FLAG_CHECKABLE);      //设为不可点击，防止创建多次
    lv_obj_clear_flag(title.back_btn, LV_OBJ_FLAG_HIDDEN);      //显示标题栏返回label
    setting_gui_wifi_panel_create();        //创建wifi面板
}

/*
 *@brief  标题返回labei点击事件回调函数
 */
static void title_back_event_cb(lv_event_t *e)
{
    if(c_title != NULL)
    {
        lv_obj_del(c_title);
        c_title = NULL;
    }

    if(wifi_list != NULL)
    {
        lv_obj_t *btn = lv_obj_get_child(wifi_list, -1);
        while(btn != NULL)
        {
            lv_obj_t *label = lv_obj_get_user_data(btn);
            void *info = lv_obj_get_user_data(label);
            if(info != NULL)
                free(info);         //释放wifi信息数据内存
            
            lv_obj_del(btn);
            btn = lv_obj_get_child(wifi_list, -1);
        }

        lv_obj_del(wifi_list);
        wifi_list = NULL;
    }

    lv_obj_t *title_back = lv_event_get_target(e);
    lv_obj_t *wifi_btn = lv_obj_get_user_data(title_back);

    lv_obj_add_flag(title.back_btn, LV_OBJ_FLAG_HIDDEN);      //隐藏标题栏返回label
    lv_obj_clear_flag(wifi_btn, LV_OBJ_FLAG_CHECKABLE);      //设为可点击
}

/*
 *@brief  wifi列表 btn点击事件回调函数
 */
static void wifi_list_btn_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);

    setting_gui_wifi_mssage_box_create(btn);            //创建一个wifi信息框
}

/*
 *@brief  wifi列表 btn消息框打开时，top layer点击事件回调函数
 */
static void wifi_list_btn_messagebox_top_layer_event_cb(lv_event_t *e)
{
    lv_obj_t *top_layer = lv_event_get_target(e);
    lv_obj_t *cont = lv_event_get_user_data(e);
    lv_obj_t *keyboard = NULL;
    lv_obj_t *pwdtextarea = lv_obj_get_user_data(top_layer);

    if(cont != NULL)
        keyboard = lv_obj_get_user_data(cont);

    if(keyboard != NULL)        //如果键盘已弹出
    {
        lv_obj_del(keyboard);       //删除键盘
        lv_obj_set_user_data(cont, NULL);
        lv_obj_center(cont);            //将窗口归中
        lv_obj_add_event_cb(pwdtextarea, pwd_textarea_event_cb, LV_EVENT_SHORT_CLICKED, cont);
        return;
    }

    if(cont != NULL)
        lv_obj_del(cont);       //删除消息框容器

    lv_obj_remove_event_cb(top_layer, wifi_list_btn_messagebox_top_layer_event_cb);     //移除事件处理函数
    lv_obj_clear_flag(top_layer, LV_OBJ_FLAG_CLICKABLE);    
    lv_obj_set_style_bg_opa(top_layer, LV_OPA_TRANSP, LV_STATE_DEFAULT);
    lv_obj_set_user_data(top_layer, NULL);
}

/*
 *@brief  密码输入框点击事件回调函数
 */
static void pwd_textarea_event_cb(lv_event_t *e)
{
    lv_obj_t *cont = lv_event_get_user_data(e);

    lv_obj_t *pwd_textarea = lv_event_get_target(e);

    //创建输入键盘
    lv_obj_t *keyboard = lv_keyboard_create(lv_layer_top());
    lv_obj_set_size(keyboard, lcd_w, lcd_h / 2);
    lv_keyboard_set_textarea(keyboard, pwd_textarea);       //绑定文本框
    
    lv_obj_set_user_data(cont, keyboard);           //保存键盘到容器

    lv_obj_align(cont, LV_ALIGN_TOP_MID, 0, 10);        //设置对齐，将窗口往上移动

    lv_obj_remove_event_cb(pwd_textarea, pwd_textarea_event_cb);            ///移除事件回调防止多次创建键盘
}

/*
 *@brief  密码可视按钮点击事件回调函数
 */
static void pwd_btn_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    lv_obj_t *btn_label = lv_obj_get_user_data(btn);
    lv_obj_t *textarea = lv_event_get_user_data(e);

    bool pwd_mode = lv_textarea_get_password_mode(textarea);

    if(pwd_mode == true)
    {
        lv_textarea_set_password_mode(textarea, false);         //切换非密码模式
        lv_label_set_text(btn_label, LV_SYMBOL_EYE_OPEN);       //切换图标表示密码可视
    }
    else
    {
        lv_textarea_set_password_mode(textarea, true);         //切换密码模式
        lv_label_set_text(btn_label, LV_SYMBOL_EYE_CLOSE);       //切换图标表示密码可视
    }
}

/*
 *@brief  wifi信息面板连接按钮点击事件回调函数
 */
static void connect_btn_event_cb(lv_event_t *e)
{
    lv_obj_t *connect_btn = lv_event_get_target(e);

    lv_obj_t *btn_label = lv_obj_get_user_data(connect_btn);
    const char *label_text = lv_label_get_text(btn_label);

    lv_obj_t *connect_wifi_btn = lv_event_get_user_data(e);
    lv_obj_t *connect_wifi_btn_label = lv_obj_get_user_data(connect_wifi_btn);
    WifiInfo *info = lv_obj_get_user_data(connect_wifi_btn_label);

    if(strcmp(label_text, "连接") == 0)
    {
        if(connected_wifi_btn != NULL)
        {
            lv_obj_t *wifi_btn_label = lv_obj_get_user_data(connected_wifi_btn);        //获取原本的已连接wifi
            WifiInfo *connected_wifi_info = lv_obj_get_user_data(wifi_btn_label);        //获取原本的已连接wifi info
            lv_label_set_text(wifi_btn_label, "\xEF\x80\xa3");       //设置锁图标，表示未连接
            connected_wifi_info->is_connected = false;
        }
        connected_wifi_btn = connect_wifi_btn;       //保存已连接的wifi list btn

        lv_obj_t *textarea = lv_obj_get_user_data(lv_layer_top());
        if(textarea != NULL)
        {
            const char *pwd = lv_textarea_get_text(textarea);       //获取文本框密码
            if(strcmp(info->psk, pwd) != 0)         //若已保存的密码与输入密码不相同或者是新的wifi
            {
                strcpy(info->psk, pwd);     //以文本框密码为准
                if(gui_opts.change_pwd != NULL && info->id >= 0)        //如果是已保存的wifi
                    gui_opts.change_pwd(info->id, info->psk);       //修改密码
            }
        }

        lv_event_send(lv_layer_top(), LV_EVENT_CLICKED, NULL);        //向顶层发送click事件，删除wifi信息面板

        if(gui_opts.connect_ap != NULL)
            gui_opts.connect_ap(info->id, info->essid, info->psk);      //连接wifi
    }
    else if(strcmp(label_text, "断开连接") == 0)
    {
        if(gui_opts.disconnect_ap != NULL)
            gui_opts.disconnect_ap(info->id);
    }
}

/*
 *@brief  定时删除对象定时器回调函数
 */
static void auto_del_obj_timer_cb(lv_timer_t *t)
{
    lv_obj_t *obj = t->user_data;

    if(obj == connecting_cont)
    {
        lv_obj_set_style_bg_opa(lv_layer_top(), LV_OPA_TRANSP, LV_STATE_DEFAULT);     //top层背景透明度
        connecting_cont = NULL;
        lv_obj_del_async(obj);
    }
}